String Class methods in Java

    String Class methods

    java.lang.String provides us with many methods that allow us to perform various operations on strings like rimming, converting, comparing, concatenating, replacing and much more. Below, the methods are discussed with examples.

    toUpperCase() and toLowerCase()-

    Example-

     class Simple{  
    
     public static void main(String args[]){  
    
    String s="Welcome Here";
    
    System.out.println(s.toUpperCase());
    
    System.out.println(s.toLowerCase());
    
     }  
    
    } 

    String trim() method-

    This method will remove the space from after and before the string.

    Example-

    class Simple{  
    
     public static void main(String args[]){  
    
    String s="    Welcome Here   ";
    
    System.out.println(s);
    
    System.out.println(s.trim());
    
     }  
    
    } 

    Output-

    Java String Trim method

    startsWith() and endsWith() method-

    Example-

    class Simple{  
    
     public static void main(String args[]){  
    
    String s="Welcome Here";
    
    System.out.println(s.startsWith("We"));
    
    System.out.println(s.endsWith("e"));
    
     }  
    
    } 

    Output-

    charAt() method-

    This method will provide you with the character at the specific index.

    Example-

    class Simple{  
    
     public static void main(String args[]){  
    
    String s="Welcome Here";
    
    System.out.println(s.charAt(0));
    
    System.out.println(s.charAt(3));
    
     }  
    
    } 

    Output-

    length() method-

    This method will allow you to provide the length of the string.

    Example-

    class Simple{  
    
     public static void main(String args[]){  
    
    String s="Welcome Here";
    
    System.out.println(s.length());
    
     }  
    } 

    Output-

    intern() method-

    Example-

    class Simple{  
    
     public static void main(String args[]){  
    
    String s=new String("Welcome Here");
    
    String s1=s.intern();
    
    System.out.println(s1);
     }  
    } 

    Output-

    valueOf() method-

    Example-

    class Simple{  
    
    static int a=15;
    
     public static void main(String args[]){  
    
    String s=String.valueOf(a);
    
    System.out.println(s+10);
    
     }  
    
    } 

    Output-

    replace() method-

    This method will allow you to replace the second string with the first string

    Example-

    class Simple{  
    
     public static void main(String args[]){  
    
    String s="Welcome, hello you are here";
    
    String s1= s.replace("hello","bye");
    
    System.out.println(s1);
    
     }  
    } 

    Output-